//package com.wiloon.java8x.datadictionary;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
//import javax.persistence.EntityManager;
//import javax.persistence.PersistenceContext;
//import javax.persistence.Query;
//
//import com.wiloon.java8x.dataImport.DataItem;
//import com.wiloon.java8x.dataImport.DataCell;
//import com.wiloon.java8x.dataImport.DataRow;
//import org.hibernate.SQLQuery;
//import org.hibernate.criterion.CriteriaSpecification;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Repository;
//
//@Repository
//public class DictionaryDaoImpl implements DictionaryDao {
//    private static Logger logger = LoggerFactory
//            .getLogger(DictionaryDaoImpl.class);
//    @PersistenceContext
//    private EntityManager em;
//
//    @Override
//    public List<DatabaseColumn> readColumnsByTable(String tableName) {
//        logger.debug("readColumnsByTable; table=" + tableName);
//        List<DatabaseColumn> columns = new ArrayList<>();
//        try {
//
//            String sql = "select c.table_name," +
//                    "       c.COLUMN_NAME," +
//                    "       c.DATA_TYPE," +
//                    "       c.DATA_LENGTH," +
//                    "       c.NULLABLE," +
//                    "       c.DATA_DEFAULT," +
//                    "       cc.comments," +
//                    " uc.constraint_type" +
//                    "  from user_tab_columns c" +
//                    "  join USER_COL_COMMENTS cc" +
//                    "    on c.table_name = cc.table_name" +
//                    "   and c.column_name = cc.column_name" +
//                    "  left join user_cons_columns ucc" +
//                    "    on c.table_name = ucc.table_name" +
//                    "   and c.COLUMN_NAME = ucc.column_name" +
//                    "  left join user_constraints uc" +
//                    "    on ucc.constraint_name = uc.constraint_name" +
//                    " where c.TABLE_NAME ='" + tableName + "'" + " and (uc.constraint_type = 'P' or uc.constraint_type is null) ";
//
//
//            Query query = em.createNativeQuery(sql);
//
//            SQLQuery nativeQuery = query.unwrap(SQLQuery.class);
//            nativeQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
//            List<Map<String, Object>> retVal = nativeQuery.list();
//            logger.debug("map size=" + retVal.size());
//
//            for (Map<String, Object> map : retVal) {
//                DatabaseColumn column = new DatabaseColumn();
//                column.setColumnName((String) map.get("COLUMN_NAME"));
//                column.setDataType((String) map.get("DATA_TYPE"));
//                column.setDataLength(map.get("DATA_LENGTH").toString());
//                column.setNullable((String) map.get("NULLABLE"));
//                column.setDefaultValue(map.get("DATA_DEFAULT") == null ? "" : map.get("DATA_DEFAULT").toString());
//                column.setComments((String) map.get("COMMENTS"));
//                column.setKey((String) map.get("CONSTRAINT_TYPE"));
//                columns.add(column);
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            logger.error("failed to get table list;" + e);
//        }
//        return columns;
//    }
//
//    @Override
//    public List<DatabaseTable> readAllTables() {
//
//        logger.debug("read all tables");
//        List<DatabaseTable> tables = new ArrayList<>();
//
//        try {
//            String sql = "select t.table_name,tc.comments from user_tables t join user_tab_comments tc on t.table_name=tc.table_name";
//            Query query = em.createNativeQuery(sql);
//            SQLQuery nativeQuery = query.unwrap(SQLQuery.class);
//            nativeQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
//            List<Map<String, String>> retVal = nativeQuery.list();
//            logger.debug("table size=" + retVal.size());
//            for (Map<String, String> map : retVal) {
//                DatabaseTable databaseTable = new DatabaseTable();
//                databaseTable.setTableName(map.get("TABLE_NAME"));
//                databaseTable.setComments(map.get("COMMENTS"));
//                tables.add(databaseTable);
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            logger.error("failed to get table list;" + e);
//        }
//        return tables;
//    }
//
//    @Override
//    public void insertRow(DataItem dataItem) {
//
//        StringBuffer keys = new StringBuffer();
//        StringBuffer valuesPlaceholder = new StringBuffer();
//
//
//        if (dataItem.hasSequencedColumn()) {
//            keys.append(dataItem.getSequenceColumn());
//        }
//        for(int i=0 ;i<dataItem.getValues().size();i++){
//            if (keys.length() != 0) {
//                keys.append(",");
//                valuesPlaceholder.append(",");
//            }
//            keys.append(dataItem.getValues().get(i).getColumnMap().getDatabaseColumn().getColumnName());
//            valuesPlaceholder.append("?");
//        }
//
//        StringBuffer sql = new StringBuffer();
//        sql.append("insert into ").append(dataItem.getTableName()).append(" (").append(keys).append(") ").append(" values ").append("( ").append(valuesPlaceholder).append(" )");
//        logger.debug("sql=" + sql);
//        Query query = em.createNativeQuery(sql.toString());
//        int index = 1;
//        for (DataCell dataCell : dataItem.getValues()) {
//            logger.debug("value=" + dataCell.toString());
//            query.setParameter(index++, dataCell.toString());
//        }
//        query.executeUpdate();
//    }
//
//    @Override
//    public void insertDataSet(DatabaseTable targetTable, List<DataRow>  resultData) {
//        for (int i=0;i<resultData.size();i++){
//            DataRow dataRow = resultData.get(i);
//            DataItem dataItem = new DataItem();
//            dataItem.setTableName(targetTable.getTableName());
//            dataItem.setValues(dataRow.getDataCells());
//            insertRow(dataItem);
//        }
//    }
//}
